/*
 * Unit test program for the Packet hierarchy.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import egs.packets.*;

import java.util.LinkedList;

public class TestPackets {

    public boolean compareRaw( byte[] before, byte[] after ) {
        if ( before.length != after.length ) return false;
        for ( int i = 0; i < before.length; i++ )
            if ( before[i] != after[i] ) return false;
        return true;
    }
    
    public static void main( String argv[] ) throws Exception {
        
        byte[] raw;
        
        System.out.println( "  ####### EGS PACKET UNIT TEST PROGRAM #######" );
        
        /**********************************************************************/
        /*************************** UseSecurePacket **************************/
        /**********************************************************************/
        
        System.out.println( "\n******** UseSecurePacket ("+Packet.MSG_USE_SECURE+") ********" );
        System.out.println( "Setting security_code " + egs.packets.UseSecurePacket.SecurityCode.tls.value );
        egs.packets.UseSecurePacket us_p = new egs.packets.UseSecurePacket( UseSecurePacket.SecurityCode.tls );
        System.out.println( "*** BEFORE Conversion: ***\n" + us_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = us_p.getRaw();
        us_p = new UseSecurePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + us_p.toString() );
        
        /**********************************************************************/
        /********************* UseSecureResponsePacket ************************/
        /**********************************************************************/
        
        System.out.println( "\n******** UseSecureResponsePacket ("+Packet.MSG_USE_SECURE_RESPONSE+") ********" );
        egs.packets.UseSecureResponsePacket.ResponseCode usr_rc = egs.packets.UseSecureResponsePacket.ResponseCode.connection_error;
        System.out.println( "Setting response_code " + usr_rc.value );
        egs.packets.UseSecureResponsePacket usr_p = new egs.packets.UseSecureResponsePacket( usr_rc );
        System.out.println( "*** BEFORE Conversion: ***\n" + usr_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = usr_p.getRaw();
        usr_p = new UseSecureResponsePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + usr_p.toString() );
        
        
        
        /**********************************************************************/
        /*************************** AuthLoginPacket **************************/
        /**********************************************************************/
        
        System.out.println( "\n********* AuthLoginPacket ("+Packet.MSG_AUTH_LOGIN+") ********" );
        short[][] game_list = new short[2][2];
        game_list[0][0] = Packet.GAME_NAME_CHECKERS;
        game_list[0][1] = 1;
        game_list[1][0] = Packet.GAME_NAME_BLACKJACK;
        game_list[1][1] = 1;
        egs.packets.AuthLoginPacket al_p = 
            new egs.packets.AuthLoginPacket( "jwalsh",
                                             "foobar",
                                             game_list );
        System.out.println( "*** BEFORE Conversion: ***\n" + al_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = al_p.getRaw();
        al_p = new AuthLoginPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + al_p.toString() );
        
        
        /**********************************************************************/
        /****************** AuthLoginErrPacket ********************************/
        /**********************************************************************/
        
        System.out.println( "\n******** AuthLoginErrPacket ("+Packet.MSG_AUTH_LOGIN_ERR+") ********" );
        System.out.println( "Setting reason_code " + egs.packets.AuthLoginErrPacket.ReasonCode.protocol_error.value );
        egs.packets.AuthLoginErrPacket ale_p = new egs.packets.AuthLoginErrPacket( AuthLoginErrPacket.ReasonCode.protocol_error );
        System.out.println( "*** BEFORE Conversion: ***\n" + ale_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = ale_p.getRaw();
        ale_p = new AuthLoginErrPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + ale_p.toString() );
        
        
        
        
        /**********************************************************************/
        /********************** GameListPacket ********************************/
        /**********************************************************************/
        System.out.println( "\n******** GameListPacket ("+Packet.MSG_GAME_LIST+") ********" );
        egs.packets.GameListPacket gl_p = new egs.packets.GameListPacket( game_list );
        System.out.println( "*** BEFORE Conversion: ***\n" + gl_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = gl_p.getRaw();
        gl_p = new GameListPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + gl_p.toString() );
        
        
        /**********************************************************************/
        /********************** SelectGamePacket ******************************/
        /**********************************************************************/
        System.out.println( "\n******** SelectGamePacket ("+Packet.MSG_SELECT_GAME+") ********" );
        egs.packets.SelectGamePacket sg_p = new egs.packets.SelectGamePacket( (short)4, Packet.GAME_NAME_BLACKJACK, (short)1 );
        System.out.println( "*** BEFORE Conversion: ***\n" + sg_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = sg_p.getRaw();
        sg_p = new SelectGamePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + sg_p.toString() );
        
        
        /**********************************************************************/
        /********************** InvalidGamePacket *****************************/
        /**********************************************************************/
        System.out.println( "\n******** InvalidGamePacket ("+Packet.MSG_INVALID_GAME+") ********" );
        egs.packets.InvalidGamePacket.ReasonCode rc = egs.packets.InvalidGamePacket.ReasonCode.resource_shortage;
        
        System.out.println( "Setting reason_code " + rc.value );
        
        egs.packets.InvalidGamePacket ig_p = new egs.packets.InvalidGamePacket( rc, Packet.GAME_NAME_BLACKJACK, (short)1 );
        System.out.println( "*** BEFORE Conversion: ***\n" + ig_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = ig_p.getRaw();
        ig_p = new InvalidGamePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + ig_p.toString() );
        
        
        /**********************************************************************/
        /***************************** OppList ********************************/
        /**********************************************************************/
        System.out.println( "\n******** OppList ("+Packet.MSG_OPP_LIST+") ********" );
        short game_name = Packet.GAME_NAME_BLACKJACK;
        short game_version = 1;
        LinkedList<String> opp_list = new LinkedList<String>();
        opp_list.add( "jwalsh" );
        opp_list.add( "rgrimsha" );
        
        egs.packets.OppListPacket ol_p = new egs.packets.OppListPacket( game_name, game_version, opp_list );
        System.out.println( "*** BEFORE Conversion: ***\n" + ol_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = ol_p.getRaw();
        ol_p = new OppListPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + ol_p.toString() );
        
        
        
        
        /**********************************************************************/
        /*************************** OppChoicePacket **************************/
        /**********************************************************************/
        
        System.out.println( "\n********* OppChoicePacket ("+Packet.MSG_OPP_CHOICE+") ********" );
        String user_name = "jwalsh";
        boolean wait = false;
        System.out.println( "Setting username to: \"" + user_name + "\" and wait to: " + wait );
        egs.packets.OppChoicePacket oc_p = 
            new egs.packets.OppChoicePacket( game_name, game_version,
                                             wait, user_name );
        System.out.println( "*** BEFORE Conversion: ***\n" + oc_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = oc_p.getRaw();
        oc_p = new OppChoicePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + oc_p.toString() );
        
        
        
        /**********************************************************************/
        /************************* OppOfferPacket *****************************/
        /**********************************************************************/
        
        System.out.println( "\n********* OppOfferPacket ("+Packet.MSG_OPP_OFFER+") ********" );
        user_name = "beata";
        System.out.println( "Setting username to: \"" + user_name +"\"" );
        egs.packets.OppOfferPacket oo_p = 
            new egs.packets.OppOfferPacket( game_name, game_version,
                                            user_name );
        System.out.println( "*** BEFORE Conversion: ***\n" + oo_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = oo_p.getRaw();
        oo_p = new OppOfferPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + oo_p.toString() );
        
        
        /**********************************************************************/
        /*************************** OppNackPacket ****************************/
        /**********************************************************************/
        
        System.out.println( "\n******** OppNackPacket ("+Packet.MSG_OPP_NACK+") ********" );
        egs.packets.OppNackPacket.ReasonCode on_rc = egs.packets.OppNackPacket.ReasonCode.opp_timeout;
        System.out.println( "Setting reason_code " + on_rc.value );
        egs.packets.OppNackPacket on_p = new egs.packets.OppNackPacket( on_rc );
        System.out.println( "*** BEFORE Conversion: ***\n" + on_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = on_p.getRaw();
        on_p = new OppNackPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + on_p.toString() );
        
        
        /**********************************************************************/
        /************************* OppAckPacket *****************************/
        /**********************************************************************/
        
        System.out.println( "\n********* OppAckPacket ("+Packet.MSG_OPP_ACK+") ********" );
        user_name = "bob";
        System.out.println( "Setting username to: \"" + user_name +"\"" );
        egs.packets.OppAckPacket oa_p = 
            new egs.packets.OppAckPacket( game_name, game_version,
                                          user_name );
        System.out.println( "*** BEFORE Conversion: ***\n" + oa_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = oa_p.getRaw();
        oa_p = new OppAckPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + oa_p.toString() );
        
        
        
        /**********************************************************************/
        /************************ BoardSendPacket *****************************/
        /**********************************************************************/
        
        System.out.println( "\n********* BoardSendPacket ("+Packet.MSG_BOARD_SEND+") ********" );
        
        int board[][] = new int[5][5];
        
        board[4][3] = 2;
        board[2][1] = 9;
        board[4][0] = 4;
        
        int player_number = 2;
        System.out.println( "Setting player_number = " + player_number );
        
        egs.packets.BoardSendPacket bs_p = 
            new egs.packets.BoardSendPacket( game_name, game_version, 
                                             player_number, board );
        System.out.println( "*** BEFORE Conversion: ***\n" + bs_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = bs_p.getRaw();
        bs_p = new BoardSendPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + bs_p.toString() );
        
        
        /**********************************************************************/
        /************************ MoveSendPacket ******************************/
        /**********************************************************************/
        
        System.out.println( "\n********* MoveSendPacket ("+Packet.MSG_MOVE_SEND+") ********" );
        
        board = new int[5][5];
        
        board[4][3] = 1;
        board[2][1] = 2;
        board[4][0] = 3;
        
        egs.packets.MoveSendPacket ms_p = 
            new egs.packets.MoveSendPacket( game_name, game_version, board );
        System.out.println( "*** BEFORE Conversion: ***\n" + ms_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = ms_p.getRaw();
        ms_p = new MoveSendPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + ms_p.toString() );
        
        
        
        /**********************************************************************/
        /*********************** MoveResponsePacket ***************************/
        /**********************************************************************/
        
        System.out.println( "\n******** MoveResponsePacket ("+Packet.MSG_MOVE_RESPONSE+") ********" );
        short mr = MoveResponsePacket.MOVE_RESPONSE_INVALID_BOARD;
        System.out.println( "Setting response_code " + mr );
        egs.packets.MoveResponsePacket mr_p = new egs.packets.MoveResponsePacket( mr );
        System.out.println( "*** BEFORE Conversion: ***\n" + mr_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = mr_p.getRaw();
        mr_p = new MoveResponsePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + mr_p.toString() );
        
        
        /**********************************************************************/
        /*************************** EndGamePacket **************************/
        /**********************************************************************/
        
        System.out.println( "\n********* EndGamePacket ("+Packet.MSG_END_GAME+") ********" );
        egs.packets.EndGamePacket.ReasonCode eg_rc = egs.packets.EndGamePacket.ReasonCode.opp_timeout;
        System.out.println( "Setting reason_code " + eg_rc.value );
        
        egs.packets.EndGamePacket eg_p = 
            new egs.packets.EndGamePacket( game_name, game_version, eg_rc );
        System.out.println( "*** BEFORE Conversion: ***\n" + eg_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = eg_p.getRaw();
        eg_p = new EndGamePacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + eg_p.toString() );
        
        

        /**********************************************************************/
        /************************ DisconnectPacket ****************************/
        /**********************************************************************/
        
        System.out.println( "\n******** DisconnectPacket ("+Packet.MSG_DISCONNECT+") ********" );
        egs.packets.DisconnectPacket.ReasonCode dp_rc = egs.packets.DisconnectPacket.ReasonCode.client_quit;
        System.out.println( "Setting reason_code " + dp_rc.value );
        egs.packets.DisconnectPacket d_p = new egs.packets.DisconnectPacket( dp_rc );
        System.out.println( "*** BEFORE Conversion: ***\n" + d_p.toString() );
        
        /*dumping to a raw buffer and back*/
        raw = d_p.getRaw();
        on_p = new OppNackPacket( raw );
        System.out.println( "*** AFTER Conversion: ***\n" + d_p.toString() );
        


        /**********************************************************************/
        /**********************************************************************/
        /**********************************************************************/
        /**********************************************************************/
        System.out.println( "\n********* PACKET TEST COMPLETE ********\n\n" );
        
    }
}
